package web;

/**
 * @author Lenovo
 * @date 2022/1/5
 */
//主要功能
import com.Dao.Question;
import com.Dao.StudentDao;
import com.Dao.UserDao;
import com.bean.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;

import static web.LoginFrame.*;
import static web.StudentFileFrame.exitButton;

public class MainFrame {

    //创造全局变量，并且私有
    private static JFrame frame = new JFrame();
    private static JPanel panel = null;
    private int leftX = 0; //设置窗口大小参数
    private int leftY = 0;
    private int width = 900;
    private int height = 700;
    private static Map<String,String> user_info;

    public MainFrame() {
        frame = new JFrame("档案查询系统");
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕尺寸
        this.width=webUtil.FRAME_WIDTH;
        this.height=webUtil.FRAME_HEIGHT;
        leftX = (d.width-width)/2;
        leftY = (d.height-height)/2;//窗口的坐标和尺寸，这种方式居中
        frame.setBounds(leftX,leftY, width, height);
        //作用：使窗体不能放大；注释：其实也可以，大部分是靠比例算的，少部分是用具体的数值确定组件大小
       frame.setResizable(false);

        //初始化页面
        panel = new JPanel();
        frame.add(panel);
        setInitLoginPanel();
//        setFilePanel();
        //使窗体可见
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static JFrame returnJFrame() {
        return frame;
    }

   public void setInitLoginPanel(){
       //加载用户数据
        UserDao dao = new UserDao();
        List<User>users = dao.queryUser();
        user_info = new HashMap<>();
        for(User user:users){
            if(!user_info.containsKey(user.getAccount()))
                user_info.put(user.getAccount(),user.getPassword());
            else
                System.out.println(user.getAccount()+"，该账户已经存在，不允许增加");
        }
       frame.remove(panel);
       panel =  new LoginFrame().loginView();
       frame.repaint();//重画
       sureButton.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               String account = LoginFrame.accountArea.getText().trim();
               String password = LoginFrame.passwordArea.getText().trim();
               String answer = LoginFrame.inputAnswerArea.getText().trim();
               if (account.equals("")) {
                   LoginFrame.accountInfoLabel.setText("输入的账户不能为空");updateQuestion();
               } else if (password.equals("")) {
                   LoginFrame. passwordInfoLabel.setText("输入的口令不能为空");updateQuestion();
               } else if (answer.equals("")) {
                   LoginFrame. answerInfoLabel.setText("计算结果不能为空");updateQuestion();
               } else if (!user_info.containsKey(account)) {
                   LoginFrame.accountInfoLabel.setText("输入的账户不存在");updateQuestion();
               }else if(!password.equals(user_info.get(account))){
                   LoginFrame.passwordInfoLabel.setText("输入的口令不对");updateQuestion();
               }else if(!answer.equals(""+ Question.getExpectAnswer())){
                   LoginFrame.answerInfoLabel.setText("计算结果不能正确");
                   try {
                       Thread.sleep(30);//留下一点时间给用户看清上次的错误
                   } catch (InterruptedException ex) {
                       ex.printStackTrace();
                   }
                   updateQuestion();
               }else{
                   System.out.println("正确，通过检查");
                   setFilePanel();
                   frame.repaint();//重画
               }
           }
       });
       cancelButton.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               frame.dispose();
           }
       });
   }

    public static void updateQuestion(){
        new Question().newGame();
        String tmpString = new Question().toString();
        questionLabel.setText(tmpString+" = ");
        frame.repaint();
    }

    public static void setFilePanel(){
        frame.remove(panel);
        StudentDao dao = new StudentDao();
        int totalNum = dao.queryStudentCount();
        panel =  new StudentFileFrame().StudentFileFrame(totalNum);
        frame.repaint();//重画
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
    }

}
